package Datos;

import Entidades.Datos;
import Interfaces.IDatos;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
// Mathias Calache: profe yo tuve que comitiar lo de rafa porque no le anda el netbeans
/**
 * 
 *
 * @author Rafael Castelo
 */
public class DBDatos implements IDatos {

    private Connection connection=new MiConexion().getConection();

    @Override
    public boolean ingreso(Datos dato) {
        try {
            CallableStatement statement;
            statement = connection.prepareCall("{call AgregaClientes (?,?,?,?,?)}");
            statement.setString(1, dato.getapellido());
            statement.setString(2, dato.getdir());
            statement.setString(3, dato.getemail());
            statement.setString(4, dato.getnombre());
            statement.setString(5, dato.gettel());

            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al agregar calificaciones");
            ex.printStackTrace();
            return false;
        }
        return true;

    }

    @Override
    public boolean modificar(Datos dato) {
        try {
            CallableStatement statement;
            statement = connection.prepareCall("{call ModificaClientes (?,?,?,?,?,?)}");
            statement.setInt(1, dato.getid());
            statement.setString(5, dato.getnombre());
            statement.setString(2, dato.getapellido());
            statement.setString(3, dato.getdir());
            statement.setString(4, dato.getemail());            
            statement.setString(6, dato.gettel());

            statement.executeUpdate();
        } catch (Exception ex) {
           //System.out.println("error al modificar calificaciones");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean eliminar(int id) {
        try {
            CallableStatement statement;
            statement = connection.prepareCall("{call EliminaClientes (?)}");
            statement.setInt(1, id);
            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al eliminar calificacion");

            return false;
        }
        return true;
    }

    @Override
    public ArrayList<Datos> lista(String filtro) {
        ArrayList<Datos> per = new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call listacliente(?)}");
            statement.setString(1, filtro);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Datos reglam = new Datos();

                reglam.setid(resultSet.getInt(1));
                reglam.setapellido(resultSet.getString(2));
                reglam.setdir(resultSet.getString(3));
                reglam.setemail(resultSet.getString(4));
                reglam.setnombre(resultSet.getString(5));
                reglam.settel(resultSet.getString(6));

                per.add(reglam);

            }
            return per;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }
    }

    @Override
    public Datos listafiltro(String filtro) {
        try {
            CallableStatement statement = connection.prepareCall("{call FiltroCLientes(?)}");
            ResultSet resultSet = statement.executeQuery();
            statement.setString(1, filtro);
            Datos h = new Datos();

            h.setid(resultSet.getInt(1));
            h.setapellido(resultSet.getString(2));
            h.setdir(resultSet.getString(3));
            h.setemail(resultSet.getString(4));
            h.setnombre(resultSet.getString(5));
            h.settel(resultSet.getString(6));
            return h;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }
    }

    @Override
    public ArrayList<Datos> Nombre(String filtro) {
       ArrayList<Datos> per = new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call FiltroClientesNombre(?)}");
            statement.setString(1, filtro);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Datos reglam = new Datos();

                reglam.setid(resultSet.getInt(1));
                reglam.setapellido(resultSet.getString(2));
                reglam.setdir(resultSet.getString(3));
                reglam.setemail(resultSet.getString(4));
                
                reglam.settel(resultSet.getString(5));

                per.add(reglam);

            }
            return per;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }
    }

    @Override
    public ArrayList<Datos> Numero(String filtro) {
        ArrayList<Datos> per=new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call FiltroClientesNumero(?)}");
            statement.setString(1, filtro);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Datos reglam = new Datos();

                reglam.setid(resultSet.getInt(1));
                reglam.setapellido(resultSet.getString(2));
                reglam.setdir(resultSet.getString(3));
                reglam.setemail(resultSet.getString(4));
                
                reglam.setnombre(resultSet.getString(5));

                per.add(reglam);

            }
            return per;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }
    }

    @Override
    public ArrayList<Datos> Email(String filtro) {
        ArrayList<Datos> per=new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call FiltroClientesEmail(?)}");
            statement.setString(1, filtro);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Datos reglam = new Datos();

                reglam.setid(resultSet.getInt(1));
                reglam.setapellido(resultSet.getString(2));
                reglam.setdir(resultSet.getString(3));
                reglam.setnombre(resultSet.getString(4));
                
                reglam.settel(resultSet.getString(5));

                per.add(reglam);

            }
            return per;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }
    }

    @Override
    public ArrayList<Datos> listatodo() {
ArrayList<Datos> per = new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call ListaTodoCliente()}");
            
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Datos reglam = new Datos();

                reglam.setid(resultSet.getInt(1));
                reglam.setapellido(resultSet.getString(2));
                reglam.setdir(resultSet.getString(3));
                reglam.setemail(resultSet.getString(4));
                reglam.setnombre(resultSet.getString(5));
                reglam.settel(resultSet.getString(6));

                per.add(reglam);

            }
            return per;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;

        }
        
    }

    @Override
    public boolean validoDatos() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public DefaultTableModel listaprestamos() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
